package erz.printing.police.notch;

import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

class VivoNotchScreenSupport implements INotchScreenSupport {
    private Class<?> vivoFtFeature = null;
    //表示是否有凹槽
    private final int VIVO_HAS_NOTCH_DISPLAY = 0x00000020;
            
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public boolean hasNotchInScreen(Window window) {
        try {
            if (vivoFtFeature == null) {
                ClassLoader cl = window.getContext().getClassLoader();
                vivoFtFeature = cl.loadClass("android.util.FtFeature");
            }
            if (vivoFtFeature == null) return false;

            Method get = vivoFtFeature.getMethod("isFeatureSupport", int.class);
            return (boolean)get.invoke(vivoFtFeature, VIVO_HAS_NOTCH_DISPLAY);
        } catch (Exception e) {
            return false;
        }
    }

    //vivo刘海大小固定
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public List<Rect> getNotchSize(Window window) {
        ArrayList<Rect> result = new ArrayList<>();
        Rect rect = new Rect();
        DisplayMetrics displayMetrics = window.getContext().getResources().getDisplayMetrics();
        int notchWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100f, displayMetrics);
        int notchHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 27f, displayMetrics);
        rect.left = (displayMetrics.widthPixels - notchWidth) / 2;
        rect.right = rect.left + notchWidth;
        rect.top = 0;
        rect.bottom = notchHeight;
        result.add(rect);
        return result;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void setWindowLayoutAroundNotch(Window window) {
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
        systemUiVisibility = systemUiVisibility | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        systemUiVisibility = systemUiVisibility | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        window.getDecorView().setSystemUiVisibility(systemUiVisibility);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void setWindowLayoutBlockNotch(Window window) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
        systemUiVisibility = systemUiVisibility & (~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        systemUiVisibility = systemUiVisibility & (~View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.getDecorView().setSystemUiVisibility(systemUiVisibility);
    }
}
